package jmine.tec.utils.annotation.visitor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * AnnotationReader que le os campos e metodos privados
 * 
 * @author takeshi
 * @param <R> R
 */
public class AllMembersAnnotationReader<R> extends AbstractAnnotationReader<R> {

    /**
     * {@inheritDoc}
     */
    public Collection<? extends Field> selectFields(Class<?> type) {
        return Arrays.asList(type.getDeclaredFields());
    }

    /**
     * {@inheritDoc}
     */
    public Collection<? extends Method> selectMethods(Class<?> type) {
        return Arrays.asList(type.getDeclaredMethods());
    }

    /**
     * {@inheritDoc}
     */
    public Collection<? extends Class<?>> selectTypes(Class<?> baseType) {
        List<Class<?>> list = new ArrayList<Class<?>>();
        collectInterfaces(baseType, list);
        return list;
    }

    /**
     * Coleta todas as classes
     * 
     * @param baseType {@link Class}
     * @param list {@link List}
     */
    protected void collectInterfaces(Class<?> baseType, List<Class<?>> list) {
        if (baseType == null) {
            return;
        }
        list.add(baseType);
        Class<?>[] interfaces = baseType.getInterfaces();
        if (interfaces != null) {
            for (Class<?> clazz : interfaces) {
                list.add(clazz);
            }
        }
        collectInterfaces(baseType.getSuperclass(), list);
    }

}
